<?php

class Order extends BaseController {

    public $order;

    function __construct() {
        parent::__construct();
        $this->order = new MOrder();
    }

    //保存订单
    function createOrder() {
        $data = Input::post('data');
        $data = json_decode($data);
        
        $order_data = $product = array();
        $order_data['order_no'] = '123';
        $order_data['exp_charge'] = $data->exp_charge;
        $order_data['order_name'] = $data->cus_name.'-订单-'.date('Y-m-d H:i',  time());
        $order_data['order_desc'] = json_encode($data->product);
        $order_data['cus_id'] = $data->cus_id;
        $order_data['cus_mobile'] = $data->mobile;
        $order_data['cus_name'] = $data->cus_name;
        $order_data['cus_address'] = $data->cus_address;
        $order_data['cus_discount'] = $data->discount;
        $order_data['pro_count'] = $data->count;
        $origin_price = 0;
        foreach ($data->product as $key => $value) {
            $origin_price += $value->price*$value->count;
            $product[$value->id] = $value->count;
        }
        $order_data['ori_price'] = $origin_price;
        $order_data['price'] = ($order_data['ori_price']*$data->discount)/100;
        $order_data['create_date'] = time();
        $order_data['is_delete'] = 0;
        
        //生成订单
        $result = $this->order->insert($order_data,$product);
        if($result){
            $this->jsonMsg(200, "新增订单成功",$result);
        }  else {
            $this->jsonMsg(100, "新增订单失败");
        }
    }
    
    //删除订单
    public function delOrder(){
        $id = Input::postInt('id');
        //删除订单
        $result = $this->order->del($id);
        if($result){
            $this->jsonMsg(200, "删除订单成功",$result);
        }  else {
            $this->jsonMsg(100, "删除订单失败");
        }
    }


    //订单详情
    function getInfo() {
        /*订单信息*/
        $id = Input::getInt('id');
        $order_info = $this->order->selectInfo($id);
        foreach ($order_info as $key => $value) {
            $this->assign[$key] = $value;
        }
        /*客户信息*/
        $customer = new MCustomer();
        $customer_info = $customer->selectInfo($order_info['cus_id']);
        $this->assign['customer_info'] = $customer_info;
        
        $add_info_thead = $this->thead['order_add_info'];
        $this->assign['add_info_thead'] = $add_info_thead;
        $this->assign['include_page'] = __TPL_PATH__ . 'Order/info.html';
        $template_path = __TPL_PATH__ . 'index.html';
        $this->_view->display($template_path, $this->assign);
    }
    
    //订单详情
    function getInfoNew() {
        /*订单信息*/
        $id = Input::getInt('id');
        $order_info = $this->order->selectInfo($id);
        foreach ($order_info as $key => $value) {
            $this->assign[$key] = $value;
        }
        /*客户信息*/
        $customer = new MCustomer();
        $customer_info = $customer->selectInfo($order_info['cus_id']);
        $this->assign['customer_info'] = $customer_info;
        
        $add_info_thead = $this->thead['order_add_info'];
        $this->assign['add_info_thead'] = $add_info_thead;
        $this->assign['include_page'] = __TPL_PATH__ . 'Order/info_new.html';
        $template_path = __TPL_PATH__ . 'index_new.html';
        $this->_view->display($template_path, $this->assign);
    }

    //订单列表
    function index() {
        $search = Input::get('search');
        $page = Input::getInt('page',1);
        $page = $page<1?1:$page;
        $page_size = 15;
        
        $order_count = $this->order->selectCounts($search,$page);
        $order = $this->order->selectLists($search,$page,$page_size);
        $this->assign['search'] = $search;
        $this->assign['page_count'] = ceil($order_count['ct']/$page_size);
        $this->assign['order'] = $order;
        $this->assign['include_page'] = __TPL_PATH__ . 'Order/index.html';
        $template_path = __TPL_PATH__ . 'index.html';
        $this->_view->display($template_path, $this->assign);
    }

    //新增订单
    function add() {
        //获取所有产品
        $product = new MProduct();
        $products = $product->selectProductList();
        $add_info_thead = $this->thead['order_add_info'];
        $this->assign['add_info_thead'] = $add_info_thead;
        $this->assign['products'] = $products;
        $this->assign['include_page'] = __TPL_PATH__ . 'Order/add.html';
        $template_path = __TPL_PATH__ . 'index.html';
        $this->_view->display($template_path, $this->assign);
    }

}
